@page "/project_group/add"
@using FOPS.Abstract.MetaInfo.Entity
@using FOPS.Abstract.MetaInfo.Server
@using Microsoft.AspNetCore.Components
@using FOPS.Abstract.K8S.Entity
@inject IIocManager _iocManager;
@inject NavigationManager nav;
@inject IJSRuntime js;

<div class="layui-card">
    <div class="layui-card-header">
        添加项目组
        <div class="layui-right">
            <button class="layui-btn" @onclick="GotoList"><i class="am-icon-plus"></i> 列表</button>
        </div>
    </div>
    <div class="layui-card-body" style="padding: 15px;">
        <div class="layui-form-item">
            <label class="layui-form-label">集群</label>
            <div class="layui-input-block">
                @foreach (var clusterVO in _lstCluster)
                {
                    <div class="layui-unselect layui-form-checkbox layui-form-checked" lay-skin="primary" style="cursor: auto;">
                        <input type="checkbox" @bind="_selectCluster[clusterVO.Id]"/>
                        <span>【@clusterVO.RuntimeEnvType.ToString()】@clusterVO.Name</span>
                    </div>
                }
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">项目组名称</label>
            <div class="layui-input-block">
                <input type="text" @bind="_info.Name" autocomplete="off" placeholder="请输入项目组名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-footer" style="left: 0;">
                    <button class="layui-btn" @onclick="AddInfo">添加</button>
                </div>
            </div>
        </div>
    </div>
</div>

@code{

    private readonly ProjectGroupVO _info = new() {ClusterIds =new()};
    private List<ClusterVO> _lstCluster = new();
    private Dictionary<int, bool> _selectCluster = new();
    private bool _isFirst;

    protected override async Task OnInitializedAsync()
    {
        if (_isFirst) return;
        _isFirst = true;

        _lstCluster = await _iocManager.Resolve<IClusterService>().ToListAsync()??new();
        _selectCluster = _lstCluster.ToDictionary(o => o.Id, o => false);
    }

    private async Task AddInfo()
    {
        try
        {
            _info.ClusterIds = _selectCluster.Where(o => o.Value).Select(o => o.Key).ToList();

            if (_info.Name == null || _info.ClusterIds == null || _info.ClusterIds.Count == 0)
            {
                await js.InvokeVoidAsync("layer.alert", "项目组、集群必须填写.", new {icon = 2, title = "出错了"});
            }

            await _iocManager.Resolve<IProjectGroupService>().AddAsync(_info);
            await js.InvokeVoidAsync("layer.msg", "添加成功", new {icon = 1});
            nav.NavigateTo("/project_group/list");
        }
        catch (Exception e)
        {
            await js.InvokeVoidAsync("layer.alert", e.Message, new {icon = 2, title = "出错了"});
        }
    }

    private void GotoList()
    {
        nav.NavigateTo("/project_group/list");
    }
}